package com.orange.lockscreen.layers.widget;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.Canvas;
import android.os.Message;
import android.view.View;

import com.orange.lockscreen.SlatHashMap;
import com.orange.lockscreen.common.Constants;
import com.orange.lockscreen.common.Constants.LSWidget_Type;
import com.orange.lockscreen.common.Constants.Slider_Msg;
import com.orange.lockscreen.common.LockScreenUtils;
import com.orange.lockscreen.common.LogHelper;
import com.orange.lockscreen.layers.SlatPanel;
import com.orange.lockscreen.layers.widget.WidgetSlat.OnMusicControlTouchListener;
import com.orange.lockscreen.server.LSWidgetHostView;

/**
 * 
 * WidgetSlatPanel: contains TimeDateView, event widget and music widget.
 * 
 */
public class WidgetSlatPanel extends SlatPanel implements LSWidget_Type,
		OnMusicControlTouchListener {
	private static final String	TAG					= "WidgetSlatPanel";

	private List<WidgetSlat>	mWidgetSlats		= null;
	private boolean				mMusicSlatDocked	= false;
	private int					mMusicXOffset		= 0;

	public WidgetSlatPanel(Context context) {
		super(context);
	}

	@Override
	protected void init() {
		Context context = getContext();
		int slatCount = SlatHashMap.getCount();
		mWidgetSlats = new ArrayList<WidgetSlat>(slatCount);
		for (int i = 0; i < slatCount; i++) {
			WidgetSlat slat = new WidgetSlat(context, i);
			if (i == SlatHashMap.MUSIC_WIDGET_ROWINDEX) {
				slat.setMusicControlListener(this);
			}
			addView(slat);
			mWidgetSlats.add(slat);
		}
	}

	@Override
	protected void layoutChild(View child, int rowIndex) {
		if (rowIndex == SlatHashMap.MUSIC_WIDGET_ROWINDEX) {
			if (mMusicSlatDocked) {
				int childLeft = 0;
				int childTop = SlatHashMap.getSlatTop(rowIndex);
				int childRight = mMusicXOffset;
				int childBottom = childTop
						+ SlatHashMap.getSlatHeight(rowIndex);
				if (child != null) {
					child.layout(childLeft, childTop, childRight, childBottom);
				}
			} else {
				super.layoutChild(child, rowIndex);
			}
		} else {
			super.layoutChild(child, rowIndex);
		}
	}

	public void changeSlatState(int rowIndex, int state) {
		if (mWidgetSlats != null) {
			int slatCount = mWidgetSlats.size();
			if (rowIndex < slatCount) {
				WidgetSlat slat = mWidgetSlats.get(rowIndex);
				if (slat.isShown()) {
					slat.changeState(state);
				}
			}
		}
	}

	public Bitmap getSlatDrawCache(int rowIndex) {
		Bitmap bitmap = null;
		if (mWidgetSlats != null) {
			int slatCount = mWidgetSlats.size();
			if (rowIndex < slatCount) {
				WidgetSlat slat = mWidgetSlats.get(rowIndex);
				if (slat != null) {
					try {
						bitmap = Bitmap.createBitmap(slat.getWidth(),
								slat.getHeight(), Config.ARGB_8888);
						Canvas canvas = new Canvas();
						canvas.setBitmap(bitmap);
						slat.draw(canvas);
					} catch (OutOfMemoryError e) {
						LogHelper.e(TAG, "getSlatDrawCache. ", e);
					} catch (Exception e) {
						LogHelper.e(TAG, "getSlatDrawCache. ", e);
					}
				}
			}
		}
		return bitmap;
	}

	public void invalidateWidgetSlat(int pos) {
		if (null != mWidgetSlats) {
			if (pos == 0) {
				WidgetSlat slat = mWidgetSlats.get(0);
				slat.invalidateUi();
			} else {
				WidgetSlat slat1 = mWidgetSlats.get(1);
				WidgetSlat slat2 = mWidgetSlats.get(2);
				int type1 = SlatHashMap.getSlatType(1);
				int type2 = SlatHashMap.getSlatType(2);
				if (type1 == TYPE_FIXED_NO_CONTENT
						&& type2 == TYPE_FIXED_NO_CONTENT) {
					slat1.useHighlightMask();
					slat2.useShadowMask();
				} else if (type1 == TYPE_FIXED_NO_CONTENT) {
					slat1.useNormalMask();
					if (!mMusicSlatDocked) {
						slat2.invalidateUi();
					}
				} else if (type2 == TYPE_FIXED_NO_CONTENT) {
					slat1.invalidateUi();
					slat2.useNormalMask();
				} else {
					slat1.invalidateUi();
					if (!mMusicSlatDocked) {
						slat2.invalidateUi();
					}
				}
			}
		}
	}

	public void release() {
		if (null != mWidgetSlats) {
			WidgetSlat slat = null;
			int slatCount = SlatHashMap.getCount();
			for (int i = 0; i < slatCount; i++) {
				slat = mWidgetSlats.get(i);
				slat.release();
			}
			mWidgetSlats.clear();
			mWidgetSlats = null;
		}
	}

	public void setMusicSlatState(boolean isDocked) {
		mMusicSlatDocked = isDocked;
	}

	public void addWidgetView(LSWidgetHostView hostView) {
		if (null != mWidgetSlats) {
			int type = hostView.getWidgetType();
			int pos = SlatHashMap.getPosByWidgetViewType(type);
			if (pos > -1 && pos < mWidgetSlats.size()) {
				WidgetSlat widgetSlat = mWidgetSlats.get(pos);
				if (widgetSlat != null) {
					widgetSlat.addHostView(hostView);
				}
			}
			if (type == LSWidget_Type.TYPE_MUSIC) {
				LockScreenUtils.sendBroadcast(getContext(),
						Constants.ACTION_LOCKSCREEN_ENTER);
			}
		}
	}

	public void addNativeView(View nativeView) {
		if (null != mWidgetSlats) {
			WidgetSlat slat = mWidgetSlats.get(SlatHashMap.TDW_WIDGET_ROWINDEX);
			if (null != slat && null != nativeView) {
				slat.addHostView(nativeView);
			}
		}
	}

	public void layoutWidgetSlat(int rowIndex, int xOffset) {
		if (null != mWidgetSlats) {
			WidgetSlat slat = mWidgetSlats.get(rowIndex);
			if (null != slat) {
				if (xOffset >= 0) {
					mMusicXOffset = xOffset;
				} else {
//					mMusicXOffset = getWidth();
					mMusicXOffset = SlatHashMap.getScreenWidth();
				}
				slat.requestLayout();
			}
		}
	}

	@Override
	public void onDown(int rowIndex) {
		sendMessage(Slider_Msg.MSG_CONTROL_LONG_CATCH_ON, rowIndex);
	}

	@Override
	public void onUp(int rowIndex) {
		sendMessage(Slider_Msg.MSG_CONTROL_LONG_CATCH_OFF, rowIndex);
	}

	private void sendMessage(int what, int rowIndex) {
		Message msg = mHandler.obtainMessage();
		msg.what = what;
		msg.arg1 = rowIndex;
		mHandler.sendMessage(msg);
	}
}
